<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Token;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
        header('Access-Control-Allow-Origin:*');
    }

    // public function index()
    // {
    //     return $this->view->fetch();
    // }

    // public function news()
    // {
    //     $newslist = [];
    //     return jsonp(['newslist' => $newslist, 'new' => count($newslist), 'url' => 'https://www.fastadmin.net?ref=news']);
    // }
    /**
     * 首页
     */
    public function shopIndex(){
        //十大份房
        $hose = db('goodtype')->where(['status'=>'normal'])->field('id,name,image')->select();
        //商品列表
        $bet = db('goods')->where(['status'=>'normal'])->field('id,title,price,images')->limit(15)->select();
        $data['hose'] = $hose;
        $data['bet'] = $bet;
        return json(['data'=>$data,'code'=>1,'msg'=>'操作成功']);
    }
    /**
     * 商品列表页
     */
    public function shopList(){
        //分类id
        $typeid = input('typeid');
        $where['status'] = 'normal';
        if($typeid){
            $where['goodtype_id'] = $typeid;
        }
        //分页
        $page =  input('page')?input('page'):1;
        $limit =  input('limit')?input('limit'):15;
        $bet = db('goods')->where($where)->field('id,title,price,images')->page($page,$limit)->select();
        $count = db('goods')->where($where)->count();
        $data['bet'] = $bet;
        $data['count'] = $count;
        return json(['data'=>$data,'code'=>1,'msg'=>'操作成功']);
    }
    /**
     * 商品详情页
     */
    public function goodDetail(){
        //商品信息
        $goodid = input('goodid');
        $where['id'] = $goodid;
        $bet = db('goods')->where($where)->find();
        //竞价信息
        $price = db('pricelog')->where(['good_id'=>$goodid])->order('addtime desc')->select();
        foreach($price as $k=>$v){
            if($v){
                $price[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            }
        }
        $data['bet'] = $bet;
        $data['price'] = $price;
        return json(['data'=>$data,'code'=>1,'msg'=>'操作成功']);
    }
    /**
     * 竞拍弹窗
     */
    public function bidding(){
        //获取最新的价格
        $goodid = input('goodid');
        $price = db('pricelog')->where(['good_id'=>$goodid,'status'=>1])->find();
        $bet = db('goods')->where(['id'=>$goodid])->find();
        if(!$price){
            $money = $bet['price'];
        }else{
            $money = $price['price'];
        }
        return json(['data'=>$money,'code'=>1,'msg'=>'操作成功']);
    }
    /**
     * 出价方法
     */
    public function setPrice(){
        //获取最新价格
        $goodid = input('goodid');
        $user_id = input('user_id');
        $username = input('username');
        $price = db('pricelog')->where(['good_id'=>$goodid,'status'=>1])->find();
        //判断商品是不是已经结束竞拍
        $bid = db('goods')->where(['id'=>$goodid])->find();
        if($bid['endtime']<time()){
            return json(['data'=>'','code'=>-2,'msg'=>'该商品已竞拍结束']);
        }
        //比较是不是最新的
        $newprice = input('newprice');
        if($newprice<=$price['price']){
            return json(['data'=>'','code'=>-1,'msg'=>'价格已更新，请查看最新价格']);
        }
        //判断用户的资金是否充足 DiamondNumdj
        $user = $this->getUserInfo($user_id);
        if($user['DiamondNum']<$newprice){
            return json(['data'=>'','code'=>-3,'msg'=>'钻石不足']);
        }
        //冻结用户钻石 
        $DiamondNumdj = $user['DiamondNumdj']+$newprice;
        $DiamondNum = $user['DiamondNum']-$newprice;
        //增加冻结
        $this->setUserInfo($user_id,'DiamondNumdj',$DiamondNumdj);
        //减少可用
        $this->setUserInfo($user_id,'DiamondNum',$DiamondNum);
        //解冻上一个冻结的资金
        //得到用户信息
        if ($price) {
            $userf = $this->getUserInfo($price['user_id']);
            // return json($userf);
            $fDiamondNumdj = $userf['DiamondNumdj']-$price['price'];
            $fDiamondNum = $userf['DiamondNum']+$price['price'];
            //减少冻结
            $this->setUserInfo($price['user_id'],'DiamondNumdj',$fDiamondNumdj);
            //增加可用
            $this->setUserInfo($price['user_id'],'DiamondNum',$fDiamondNum);
            //修改之前价格的状态
            $data['status'] = 2;
            $r = db('pricelog')->where(['good_id'=>$goodid,'status'=>1])->update($data);
        }
        
        
        //加入最新的价格
        $ins['user_id'] = $user_id;
        $ins['username'] = $username;
        $ins['good_id'] = $goodid;
        $ins['price'] = $newprice;
        $ins['addtime'] = time();
        $res = db('pricelog')->insert($ins);
        if($res){
            return json(['data'=>'','code'=>1,'msg'=>'操作成功']);
        }
    }
    /**
     * 倒计时结束之后 加入订单  自动执行程序
     */
    public function addOrder(){
        //获取所有正在拍卖中的商品
        $where['state'] = 1;
        $where['endtime'] = ['<',time()];
        $bet = db('goods')->where($where)->select();
        if($bet){
            foreach($bet as $k=>$v){
                //改变商品的状态
                db('goods')->where(['id'=>$v['id']])->update(['state'=>2]);
                //找到商品出的最高价格
                $price = db('pricelog')->where(['good_id'=>$v['id'],'status'=>1])->find();
                //找出 出价最高人的信息 扣除此人的冻结钻石
                $user = $this->getUserInfo($price['user_id']);
                $DiamondNumdj = $user['DiamondNumdj']-$price['price'];
                $this->setUserInfo($price['user_id'],'DiamondNumdj',$DiamondNumdj);
                //查这个人的地址信息
                $address = db('Address')->where(['user_id'=>$price['user_id']])->find();
                if($address){
                    $data['name'] = $address['name'];
                    $data['address'] = $address['address'];
                    $data['phone'] = $address['phone'];
                }
                $data['user_id'] = $price['user_id'];
                $data['linkphone'] = $user['Phone'];
                $data['good_name'] = $v['title'];
                $data['hprice'] = $price['price'];
                $data['addtime'] = time();
                db('Order')->insert($data);
            }
        }
        return json(['data'=>'','code'=>1,'msg'=>'操作成功']);
    }
}
